/*
	This file is part of GTimer.

    GTimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License\.

    GTimer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GTimer. If not, see <http://www.gnu.org/licenses/>.
	
	Copyright 2012 Muhammad Ghareeb Sideek
	Contact: MGhareeb64@gmail.com
 */

package com.gsourcecode.timer;

import java.io.File;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.gsourcecode.command.Command;
import com.gsourcecode.utils.Colors;

public class EditGTimer extends ListActivity {

	private MobileArrayAdapter adpt;
	private AlertDialog eventActionDialog;
	private int selected;
	private File f;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// READ FILE	
		ArrayList<Command> com = new ArrayList<Command>();
		f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "gtimer" + File.separator + GTimerMain.gtimerFile + ".txt");
		try {
			com.addAll(Command.parseFile(f));
			for (int i = com.size() - 1; i > 0; i--)
				com.get(i).time -= com.get(i - 1).time;
		} catch (Exception e) {}
		adpt = new MobileArrayAdapter(EditGTimer.this, com);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adpt.notifyDataSetChanged();
			}
		});
		setListAdapter(adpt);
		// ALERT
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Specify an action for the selected event");
		builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				new NewEventDialog(EditGTimer.this, adpt.values.get(selected)).show();
			}
		});
		builder.setNeutralButton("Down", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				moveDown(selected);
			}
		});
		builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				delete(selected);
			}
		});
		eventActionDialog = builder.create();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Command com = adpt.values.get(position);
		selected = position;
		// ADD
		if (com.title.equalsIgnoreCase("Add...")) {
			new NewEventDialog(EditGTimer.this, null).show();
		}
		// DONE
		else if (com.title.contains("Save")) {
			adpt.values.remove(adpt.values.size() - 1);
			adpt.values.remove(adpt.values.size() - 1);
			Command.toFile(adpt.values, f);
			finish();
		}
		// EVENT
		else {
			eventActionDialog.show();
		}
	}

	public void add(final Command com) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adpt.values.add(adpt.values.size() - 2, com);
				adpt.notifyDataSetChanged();
			}
		});
	}

	private void moveDown(final int i) {
		if (i >= 0 && i < adpt.getCount() - 3)
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Command com = adpt.values.remove(i);
					adpt.values.add(i + 1, com);
					adpt.notifyDataSetChanged();
				}
			});
	}

	private void delete(final int i) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adpt.values.remove(i);
				adpt.notifyDataSetChanged();
			}
		});
	}

	public void place(final Command com) {
		delete(selected);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adpt.values.add(selected, com);
				adpt.notifyDataSetChanged();
			}
		});
	}

	// CUSTOM ADAPTER

	public static class MobileArrayAdapter extends ArrayAdapter<String> {
		private final Context context;
		public ArrayList<Command> values;

		public MobileArrayAdapter(Context context, ArrayList<Command> values) {
			super(context, R.layout.list_fruit);
			this.values = values;
			this.values.add(new Command(0, 0, "Add...", ""));
			this.values.add(new Command(0, 0, "Save", ""));
			this.context = context;
		}

		@Override
		public int getCount() {
			return values.size();
		}

		@Override
		public String getItem(int position) {
			return values.get(position).title;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowView = inflater.inflate(R.layout.list_fruit, parent, false);
			TextView text = (TextView) rowView.findViewById(R.id.eventTitle);
			TextView dur = (TextView) rowView.findViewById(R.id.eventDur);

			dur.setText("");
			// ADD
			if (values.get(position).title.equalsIgnoreCase("Add..."))
				text.setText("Add...");
			// DONE
			else if (values.get(position).title.contains("Save"))
				text.setText("Save\'" + GTimerMain.gtimerFile + "\'");
			// EVENT
			else {
				Command com = values.get(position);
				int c = Colors.opacity(com.color, 0xBB);
				long t = com.time / 1000;
				dur.setText(String.format("%02d:%02d:%02d", (t / 60 / 60), (t / 60) % 60, (t) % 60));
				dur.setBackgroundColor(Color.parseColor("#BBFFFFFF"));
				dur.setTextColor(Color.parseColor("#FF000000"));
				text.setText(com.title + "_" + com.sub);
				text.setBackgroundColor(c);
			}
			return rowView;
		}
	}
}